//
//  WeatherVC.m
//  qulvyou
//
//  Created by mmyy on 15/10/25.
//  Copyright (c) 2015年 mmyy. All rights reserved.
//

#import "WeatherVC.h"
#import "weatherHeader.h"
#import "weatherDayItems.h"
#import "weatherCell.h"
#import "headView.h"
#import "SearchViewControll.h"

@interface WeatherVC ()<UITableViewDataSource, UITableViewDelegate, searchViewDelegate>

@end

@implementation WeatherVC
{
    UISearchController *_searchController;
    UIButton *_rightBt;
    UITableView *_tableView;
    NSMutableArray *_dataSource;
    UIView *_headView;
    weatherHeader *_headerItems;
    UIImageView *_loadImage;
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([self.currentCity isEqualToString:@"选择城市"]) {
        self.currentCity = @"北京";
    }
    [self createNavcation];
    _loadImage = [loadImage loadImage];
    [self.view addSubview:_loadImage];
    _loadImage.hidden = YES;
    self.view.backgroundColor = [UIColor purpleColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self loadData];
}
-(void)createNavcation
{
    self.navigationItem.title = @"旅游天气查询";
    _rightBt = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 60, 30)];
    [_rightBt setTitle:self.currentCity forState:UIControlStateNormal];
    [_rightBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_rightBt addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightb = [[UIBarButtonItem alloc] initWithCustomView:_rightBt];
    self.navigationItem.rightBarButtonItem = rightb;
}
-(void)buttonClick:(UIButton *)sender
{
    SearchViewControll *searchView = [[SearchViewControll alloc] init];
    searchView.delegate = self;
    searchView.currentCity = sender.currentTitle;
    [self.navigationController pushViewController:searchView animated:YES];

}
-(void)searchViewSuccess:(NSString *)searchCity
{
    
    [_rightBt setTitle:searchCity forState:UIControlStateNormal];
    self.currentCity = searchCity;
    [self loadData];
}
-(void)loadData
{
    _loadImage.center = self.view.center;
    _loadImage.hidden = NO;
    [self.view bringSubviewToFront:_loadImage];
    _dataSource = [NSMutableArray arrayWithCapacity:0];
    NSString *url = [NSString stringWithFormat:weatherUrl, self.currentCity];
    NSString *urlString = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [Funcation getWithURL:urlString success:^(id responseObject) {
        _loadImage.hidden = YES;
        if ([[responseObject objectForKey:@"error_code"] intValue] == 0) {
            if (_tableView) {
                [_tableView removeFromSuperview];
            }
            NSDictionary *result = [responseObject objectForKey:@"result"];
            NSDictionary *data = [result objectForKey:@"data"];
            _headerItems = [weatherHeader weatherHeaderWithDict:[data   objectForKey:@"life"]];
            NSArray *weather = [data objectForKey:@"weather"];
            for (NSDictionary *value in weather) {
                weatherDayItems *items = [weatherDayItems weatherDayItemsWithDict:value];
                [_dataSource addObject:items];
            }
            [self createTableView];
        }else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"查询天气失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
        
    } andFail:^(id error) {
        NSLog(@"Faile");
    }];
}
-(void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.height - 65)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 153;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.allowsSelection = NO;
    [self.view addSubview:_tableView];
    [self createTableHeadView];

}
-(void)createTableHeadView
{
    _headView = [headView getHeadViewWithWeatherHeader:_headerItems andWeatherDayItems:[_dataSource objectAtIndex:0] withFrame:CGRectMake(0, 0, self.view.frame.size.width, 250)];
    _tableView.tableHeaderView = _headView;
    

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count - 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ident = @"basis_cell";
    weatherCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"weatherCell" owner:nil options:nil] firstObject];
        cell.backgroundColor = [UIColor clearColor];
    }
    weatherDayItems *items = [_dataSource objectAtIndex:indexPath.row + 1];
    cell.riqi.text = [[[items date] componentsSeparatedByString:@"-"] componentsJoinedByString:@"."];
    cell.wendu.text = [NSString stringWithFormat:@"最高:%@ :最低%@", items.wendu, items.minWend];
    cell.fengdu.text = items.fengdu;;
    cell.fengdu.adjustsFontSizeToFitWidth = YES;
    cell.tianqi.text = items.tianqi;
    cell.shihe.text = [items.wendu intValue] - [items.minWend intValue] > 10 ? @"不适合旅游, 温差较大":@"适合旅游";
    cell.shihe.adjustsFontSizeToFitWidth = YES;
    
    return cell;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
